using System;
using System.Collections.Generic;

namespace IWS.Lib.SingleSignOn
{
	public class Setting
	{
		string m_Key;
		string m_Value;

		/// <summary>
		/// Create a new setting
		/// </summary>
		public Setting()
		{
			init();
		}

		/// <summary>
		/// Create a new instance and preload data by ID.
		/// </summary>
		/// <param name="id"></param>
		public Setting(string id)
		{
			init();
			Load(id);
		}


		private void init()
		{
			 m_Key = "";
			 m_Value = "";
		}


		#region Properties
		public string Key
		{
			get {return m_Key;}
			set {m_Key = value;}
		}
		public string Value
		{
			get {return m_Value;}
			set {m_Value = value;}
		}
		#endregion

		/// <summary>
		/// Load this setting
		/// </summary>
		public void Load(string id)
		{
			Setting data = Instance.getDataProvider().LoadSetting(id);
			this.Key = data.Key;
			this.Value = data.Value;
		}

		/// <summary>
		/// Save this setting
		/// </summary>
		/// <param name="id"></param>
		public void Save()
		{
			//TODO:	Finish ID generation code here.
			Instance.getDataProvider().SaveSetting(this);
		}

		/// <summary>
		/// Remove this setting
		/// </summary>
		/// <param name="id"></param>
		public void Remove()
		{
			Instance.getDataProvider().RemoveSetting(this.Key);
		}

		/// <summary>
		/// Check if this setting already exists.
		/// </summary>
		/// <param name="id">Setting ID</param>
		public bool Exists()
		{
			return Setting.Exists(this.Key);
		}

		/// <summary>
		/// Remove a setting
		/// </summary>
		/// <param name="ID"></param>
		public static void Remove(string ID)
		{
			Instance.getDataProvider().RemoveSetting(ID);
		}

		/// <summary>
		/// Check if this setting already exists.
		/// </summary>
		/// <param name="id">Setting ID</param>
		public static bool Exists(string ID)
		{
			return Instance.getDataProvider().CountSetting(ID) > 0;
		}

		/// <summary>
		/// List all Setting
		/// </summary>
		public static List<Setting> ListAll()
		{
			return Instance.getDataProvider().ListAllSetting();
		}

		public static Dictionary<string, string> ListAllByDictionary()
		{
			List<Setting> settings = ListAll();
			Dictionary<string, string> list = new Dictionary<string, string>();
			foreach (Setting setting in settings)
			{
				list.Add(setting.Key, setting.Value);
			}
			return list;
		}

	}
}
